package yxy.game.pm2.command.godBeast;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.GodBeastEquipPosition;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PickedGodBeastMessage;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.godBeast.GodBeast;
import yxy.game.pm2.module.godBeast.PickedGodBeastHandle;
import yxy.game.pm2.module.godBeast.PickedGodBeastModule;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;

/**
 * @name CMD_PickedGodBeastEquipChange
 * @author wrs
 * @description 神兽装备替换
 * @since 2022-05-31
 */
@MessageType(proto = Command.PickedGodBeastEquipChange,classtype = PickedGodBeastMessage.PickedGodBeastEquipChangeRequest.class)
public class CMD_PickedGodBeastEquipChange extends Command<PickedGodBeastMessage.PickedGodBeastEquipChangeRequest> {

    @Override
    public void execute(MessageSource messageSource, PickedGodBeastMessage.PickedGodBeastEquipChangeRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int code = request.getCode();//神兽ID
        int equippos = request.getEquippos(); //装备位置  11：爪；12：魂；13：环
        int equipid = request.getEquipid(); //装备ID 0为卸下 不要用-1

        PickedGodBeastMessage.PickedGodBeastEquipChangeResponse.Builder response = PickedGodBeastMessage.PickedGodBeastEquipChangeResponse.newBuilder();

        try {
            PickedGodBeastModule module = PickedGodBeastModule.get(playerCode);
            if (PickedGodBeastModule.lockPickedGodBeast(playerCode)){
                GodBeastEquipPosition equipPosition = GodBeastEquipPosition.forNumber(equippos);
                if (equipPosition == null){
                    send(messageSource, response, Result.ParamError);
                    return;
                }

                if (module == null){
                    send(messageSource, response, Result.ParamError);
                    return;
                }

                GodBeast godBeast = new PickedGodBeastHandle().getGodBeastById(module,code); //神兽
                if (godBeast == null){
                    send(messageSource, response, Result.ParamError);
                    return;
                }

                PickedGodBeastHandle handle = new PickedGodBeastHandle();
                Result result = handle.ChangeEquip(playerCode,godBeast,equipPosition,equippos,equipid);
                if (!result.equals(Result.Success)){
                    send(messageSource, response, result);
                    return;
                }
                module.set();

                for (GodBeast value : module.getGbs().values()) {
                    response.addGbs(handle.getGodBeasts(playerCode, value));
                }

                send(messageSource,response, Result.Success);

                PlayTypeModule playTypeModule = PlayTypeModule.get(playerCode);
                PlayTypeHandle.savePickedGodBeast(playerCode,playTypeModule);
                new BattleHandle().powerUpdate(playerCode);// 战力更新

                //todo 神兽红点推送
            }
        }catch (Exception e){
            e.printStackTrace();
            send(messageSource, response, Result.DataError); //数据错误
        }finally {
            PickedGodBeastModule.unlockPickedGodBeast(playerCode);
        }



    }

    private void send(MessageSource messageSource, PickedGodBeastMessage.PickedGodBeastEquipChangeResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
